package de.tu_darmstadt.gdi1.battleship.model;

import java.io.BufferedReader;
import java.io.IOException;

import de.tu_darmstadt.gdi1.battleship.exceptions.InvalidCoordinatesException;

/**
 * 
 * Fasst die x und y Koordinate zu einem Element vom Typ Coordinates zusammen.
 * 
 */
public class Coordinates {

	private int x, y;

	/**
	 * creates a new coordinate
	 * 
	 * @param x
	 *            horizontal 
	 * @param y
	 *            vertical
	 * @throws InvalidCoordinatesException
	 *             
	 */
	public Coordinates(int x, int y) throws InvalidCoordinatesException {

		if (x < 0 || x > 9 || y < 0 || y > 9) {
			throw new InvalidCoordinatesException("");
		} else {
			this.x=x;
			this.y=y;
		}

	}

	public Coordinates() {
		x=0;
		y=0;
	}
	
	
	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}
	/**
	 * checks of another coordinate is the same
	 * @param co
	 * the other coordinate
	 * @return
	 * true if they are equal
	 */
	public boolean equals(Coordinates co){
		return(x == co.getX() && y == co.getY());
	}
/**
 * saves the informations of a coordinate into a string
 * @return
 */
	public String generateSaveString() {

		StringBuffer sb = new StringBuffer("#CO\n");
		
		sb.append(x).append('\n');
		sb.append(y).append('\n');
		
		return sb.toString();
		
	}
	/**
	 * reads a coordinate save string
	 * @param br
	 * BufferedReader
	 * @throws IOException
	 */
	public void loadSaveString(BufferedReader br) throws IOException {
		
		if(br.readLine().equals("#CO")) {
			x = Integer.valueOf(br.readLine());
			y = Integer.valueOf(br.readLine());
		} else {
			System.err.println("Buffered Reader does not begin with #CO");
		}
	}
}
